package _2022.hot100._42_接雨水;

/**
 * @author： YHSimon
 * @date： 2022-04-10 14:20
 */
public class Solution {


    //双指针 0ms
    public int trap(int[] height){
        int left=0,right=height.length-1,ans=0;
        int left_max=0,right_max=0;
        while(left<=right){
            if(left_max<right_max){
                ans+=Math.max(0, left_max-height[left]);
                left_max=Math.max(left_max, height[left]);
                left++;
            }else{
                ans+=Math.max(0, right_max-height[right]);
                right_max=Math.max(right_max, height[right]);
                right--;
            }
        }
        return ans;
    }

    public static void main(String[] args) {
        Solution s = new Solution();
        System.out.println(s.trap(new int[]{0,1,0,2,1,0,1,3,2,1,2,1}));
//        System.out.println(s.trap(new int[]{4,2,0,3,2,5}));
//        System.out.println(s.trap(new int[]{4,2,3}));
    }
}
